package it.struts.helloworld.action;


import it.struts.helloworld.dao.LoginDAO;
import it.struts.helloworld.form.LoginForm;
import it.struts.helloworld.model.Utenti;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {
 
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
     
        String target = null;
        LoginForm loginForm = (LoginForm)form;
        
        LoginDAO login = new LoginDAO();
        Utenti utente = login.retrieveDetailsDao(loginForm.getUserName());
             
        if(loginForm.getUserName().equals(utente.getUsername())
                && loginForm.getPassword().equals(utente.getPassword())) {
        	target = "success";
            request.setAttribute("message", loginForm.getUserName());
        }
        else {
        	target = "failure";
        }
         
        return mapping.findForward(target);
    }
}